package com.gnerv.pabu.plugin.utils;

import com.gnerv.pabu.plugin.entity.Template;
import com.gnerv.pabu.plugin.entity.TemplateGroup;
import com.intellij.openapi.util.io.FileUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TemplateGroupUtils {

    private TemplateGroupUtils(){}

    public static TemplateGroup loadTemplate(String groupName, String groupPath, String[] fileNames, String fileType) throws IOException {
        TemplateGroup templateGroup = new TemplateGroup();
        templateGroup.setName(groupName);
        templateGroup.setPath(groupPath);
        List<Template> templates = new ArrayList<>();
        for (String fileName : fileNames) {
            String velocityTemplate = FileUtil.loadTextAndClose(TemplateGroupUtils.class.getResourceAsStream(groupPath + fileName + fileType));
            Template template = new Template(fileName, velocityTemplate, fileType);
            templates.add(template);
        }
        templateGroup.setTemplates(templates);
        return templateGroup;
    }

}
